package com.eke.dao;

import java.util.List;
import java.util.Map;

import javax.swing.text.html.parser.Entity;

import org.springframework.stereotype.Repository;

import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause.Entry;
import com.eke.bean.Comment;
import com.eke.bean.Course;
import com.eke.bean.Users;
import com.jason.dbhelper2.jdbc.BaseJdbcTemplate;

@Repository("courseDao")
public class CourseDao extends BaseJdbcTemplate<Course> {
	/**
	 * 随机查询4条数据
	 * 
	 * @return {@link List}<{@link Course}>
	 */
	public List<Course> queryRecommendCourses() {
		return this.queryForList("select t1.*,t2.user_name FROM course as t1 LEFT JOIN users as t2 On t1.teacher_id = t2.user_id ORDER BY  RAND() LIMIT 5");
	}

	/**
	 * 根据课程分类id查询6条课程信息
	 * 
	 * @param courseTypeID
	 *            课程分类id
	 * @return @{@link List}<{@link Course}>
	 */
	public List<Course> queryRecommendByCourseTypeId(Integer courseTypeID) {
		return this.queryForList("select t1.*,t2.user_name FROM course as t1 LEFT JOIN users as t2 On t1.teacher_id = t2.user_id where course_type_id = ? ORDER BY  RAND() LIMIT 6",courseTypeID);
	}

	/**
	 * 通过课程名查询课程
	 * 
	 * @param courseName
	 * @return {@link List}<{@link Course}>
	 */
	public List<Course> queryCourseByCourseName(String courseName) {
		return this.queryForList("select * from course where course_name like '%" + courseName + "%'");
	}

	/**
	 * 根据课程分类id查询课程信息
	 * 
	 * @param courseId
	 *            课程分类id
	 * @return {@link List}<{@link Course}>
	 */
	public List<Course> queryCourseById(Integer courseId, int startRow, int pageSize) {
		return this.queryForList("select t1.*,t2.user_name FROM course as t1 LEFT JOIN users as t2 On t1.teacher_id = t2.user_id where course_type_id = ? limit ?,?",courseId, startRow, pageSize);
	}

	/**
	 * 查询课程分类id为？的课程总数
	 * 
	 * @return int
	 */
	public int queryCourseTotal(Integer courseId) {
		Map<String, Object> map = this.getJdbcTemplate().queryForMap("select count(1) total from course where course_type_id=?", courseId);
		return ((Long) map.get("total")).intValue();
	}

	/**
	 * 查询所有课程信息
	 * 
	 * @param courseId
	 *            课程分类id
	 * @return {@link List}<{@link Course}>
	 */
	public List<Course> querAllyCourse(int startRow, int pageSize) {
		return this.queryForList("select t1.*,t2.user_name FROM course as t1 LEFT JOIN users as t2 On t1.teacher_id = t2.user_id  limit ?,?",startRow, pageSize);
	}

	/**
	 * 查询课程总数
	 * 
	 * @return int
	 */
	public int queryCourseTotal() {
		Map<String, Object> map = this.getJdbcTemplate().queryForMap("select count(1) total from course");
		return ((Long) map.get("total")).intValue();
	}

	/**
	 * 根据课程id查询课程信息
	 * 
	 * @param courseId
	 * @return {@link Course}
	 */
	public Course queryCourseDeatilsById(Integer courseId) {
		return this.queryForObject("select t1.*,t2.user_name FROM course as t1 LEFT JOIN users as t2 On t1.teacher_id = t2.user_id where t1.course_id = ?",courseId);
	}

	/**
	 * 上传课程
	 * 
	 * @param course
	 * @return
	 */
	public int insertCourse(Course course) {
		return this.insert(course);
	}

	/**
	 * 根据用户id查询课程总数
	 * 
	 * @return
	 */
	public int queryCourseTotalByUserId(int userId) {
		Map<String, Object> map = this.getJdbcTemplate().queryForMap("select count(1) total from course where teacher_id=?", userId);
		return ((Long) map.get("total")).intValue();
	}

	/**
	 * 根据用户id查询课程
	 * 
	 * @return
	 */
	public List<Course> queryCourseByUserId(Integer userId, int startRow, Integer pageSize) {
		return this.queryForList("select t1.*,t2.user_name FROM course as t1 LEFT JOIN users as t2 On t1.teacher_id = t2.user_id where teacher_id = ? limit ?,?",userId, startRow, pageSize);
	}

	
	
	
}
